package com.ijiuchuang.jc_localization.app;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.module.GlideModule;

/**
 * Created by Veev on 2016/6/17
 *
 * @link 384506557@qq.com
 */
public class GlideConfig implements GlideModule {

    private static final int GB = 1024 * 1024 * 1024;
    private static final int MB = 1024 * 1024;
    private static final int KB = 1024;
    private static final int B = 1;

    private static int memoryCache = (int) (Runtime.getRuntime().maxMemory() / 8);
    private static int diskCache = 1 * GB;

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        // 设置缓存大小 为 内存的 1/8
        MemorySizeCalculator calculator = new MemorySizeCalculator(context);
        int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
        int defaultBitmapPoolSize = calculator.getBitmapPoolSize();

        int customMemoryCacheSize = (int) (1.125 * defaultMemoryCacheSize);
        int customBitmapPoolSize = (int) (1.125 * defaultBitmapPoolSize);

        builder.setMemoryCache( new LruResourceCache( customMemoryCacheSize ));
        builder.setBitmapPool( new LruBitmapPool( customBitmapPoolSize ));
        // 设置SD卡缓存
        builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, diskCache));
    }

    @Override
    public void registerComponents(Context context, Glide glide) {

    }
}
